//
//  NSDataAdditions.m
//  TestFont
//
//  Created by 李军 on 13-4-10.
//  Copyright (c) 2013年 李军. All rights reserved.
//

#import "NSData+Additions.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSData (Additions)

- (NSString*)md5Hash
{
	unsigned char result[CC_MD5_DIGEST_LENGTH];
	CC_MD5([self bytes], [self length], result);
	
	return [NSString stringWithFormat:
			@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
			result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
			];
}

- (NSString *)getImeiType
{
    uint8_t c;
    [self getBytes:&c length:1];
    
    switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
    }
    return nil;

}

- (NSDictionary *)jsonSerializtion
{
    return [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingAllowFragments error:nil];
}

- (NSString *)stringLatin1
{
    return [[NSString alloc] initWithData:self encoding:NSISOLatin1StringEncoding];
}

- (NSString *)stringUTF8
{
    return [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
}

- (NSString *)stringASCII
{
    return [[NSString alloc] initWithData:self encoding:NSASCIIStringEncoding];
}

- (NSString *)stringUnicode
{
    return [[NSString alloc] initWithData:self encoding:NSUnicodeStringEncoding];
}


@end
